﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cash_n_guns
{
    class GameLogic
    {
        const int TWENTY_THOUSAND_BILLS = 10;
        const int TEN_THOUSAND_BILLS = 15;
        const int FIVE_THOUSAND_BILLS = 15;
        ArrayList AlivePlayers = new ArrayList();
        ArrayList DeadPlayers = new ArrayList();
        ArrayList MoneyPot = new ArrayList();
        bool GameOver = false;
        
        public GameLogic()
        {
            CreatePlayers();
            CreatePot();
            PlayGame();
        }

        private void CreatePlayers()
        {
            // TODO: Replace this part with network thingie...
            Player player1 = new Player();
            Player player2 = new Player();
            Player player3 = new Player();
            Player player4 = new Player();

            AlivePlayers.Add(player1);
            AlivePlayers.Add(player2);
            AlivePlayers.Add(player3);
            AlivePlayers.Add(player4);
        }

        private void CreatePot()
        {
            // Add 20.000 bills to the money pot
            for (int MoneyCount = 0; MoneyCount < TWENTY_THOUSAND_BILLS; MoneyCount++)
                MoneyPot.Add(20000);
            
            // Add 10.000 bills to the money pot
            for (int MoneyCount = 0; MoneyCount < TEN_THOUSAND_BILLS; MoneyCount++)
                MoneyPot.Add(10000);

            // Add 5.000 bills to the money pot
            for (int MoneyCount = 0; MoneyCount < FIVE_THOUSAND_BILLS; MoneyCount++)
                MoneyPot.Add(5000);

            // Shuffle the pot
            ShufflePot();
        }

        private void ShufflePot()
        {
            Random RandomGen = new Random(DateTime.Now.Millisecond);
            ArrayList RandomList = new ArrayList();
            Int32 Index;

            while (MoneyPot.Count > 0)
            {
                Index = RandomGen.Next(MoneyPot.Count);
                RandomList.Add(MoneyPot[Index]);
                MoneyPot.RemoveAt(Index);
            }

            MoneyPot = RandomList;
        }

        private void PlayGame()
        {
            // While there are at least 2 players 'alive' and less then 8 rounds played...
            while (!GameOver)
            {
                // Play one round
                Round round = new Round(AlivePlayers, GetBillsFromMoneyPot());

                // Check if any players are 'dead'
                CheckStatusOfPlayers();

                // Check if the game is over
                CheckGameStatus();
            }

            // Split the money between the 'living' players and display the results
            EndGame();
        }

        private int[] GetBillsFromMoneyPot()
        {
            // Get the bills from the moneypot...
            int[] bills = new int[5] 
            { 
                (int) MoneyPot[0], 
                (int) MoneyPot[1], 
                (int) MoneyPot[2], 
                (int) MoneyPot[3], 
                (int) MoneyPot[4] 
            };

            // Remove the bills from the moneypot...
            for (int x = 0; x < 5; x++)
                MoneyPot.RemoveAt(0);

            // Return the bills taken from the pot...
            return bills;
        }

        private void CheckStatusOfPlayers()
        {
            // Go through each player and check wether his 'wounds' have 'killed' him
            foreach (Player player in AlivePlayers)
            {
                // If the player is dead...
                if (player.WoundCount == 3)
                {
                    // Mark him and put him in the appropriate list...
                    player.IsDead = true;
                    DeadPlayers.Add(player);
                    AlivePlayers.Remove(player);
                }
            }
        }

        private void CheckGameStatus()
        {
            // If there isn't any more money or only one player 'alive', the game is over...
            if (MoneyPot.Count == 0 || AlivePlayers.Count < 2)
                GameOver = true;
        }

        private void EndGame()
        {
            // If there is only one player alive and money still in the pot...
            // Put the money in the pot into the players bank...

            // Calculate the highscore (Player score = players bank - (Shame markers * 5000) ) etc...
 
            // Show the highscore...
        }
    }
}
